<?php

/**
 *
 * @author ben <ben@spicatech.cn>  Created on 2015-7-21
 */
class WechatComponentMessage extends WechatComponent {

    private $receive = array();
    protected $messageToken = '';
    protected $messageKey = '';

    public function __construct(array $conf, $messageConf = array()) {
        parent::__construct($conf);
        if (isset($messageConf['messageToken'])) {
            $this->messageToken = $messageConf['messageToken'];
        }
        if (isset($messageConf['messageKey'])) {
            $this->messageKey = $messageConf['messageKey'];
        }
    }

    /**
     * 
     * @param type $msgSignature
     * @param type $timestamp
     * @param type $nonce
     * @param type $xml
     * @return boolean|array
     */
    public function decodeMessage($msgSignature, $timestamp, $nonce, $xml) {

        $pc = new WXBizMsgCrypt($this->messageToken, $this->messageKey, $this->appid);
        $msg = '';
        $errCode = $pc->decryptMsg($msgSignature, $timestamp, $nonce, $xml, $msg);
        if ($errCode == 0) {
            $this->receive = XML::xmlToArray($msg);
            return $this->receive;
        }
        $this->errcode = $errCode;
        $this->errmsg = 'Failed';
        return false;
    }

    /**
     * 
     * @param type $xml
     * @return boolean|string
     */
    public function encodeMessage($xml) {
        $pc = new WXBizMsgCrypt($this->messageToken, $this->messageKey, $this->appid);
        $msg = '';
        $timeStamp = time();
        $nonce = uniqid('', true);
        $errCode = $pc->encryptMsg($xml, $timeStamp, $nonce, $msg);
        if ($errCode == 0) {
            return $msg;
        }

        $this->errcode = $errCode;
        $this->errmsg = 'Failed';
        return false;
    }

    public function getReceiveEvent() {
        return isset($this->receive['Event']) ? $this->receive['Event'] : '';
    }

    public function getReceiveType() {
        return isset($this->receive['MsgType']) ? $this->receive['MsgType'] : '';
    }

    public function getReceiveContent() {
        return isset($this->receive['Content']) ? $this->receive['Content'] : '';
    }

    public function getReceiveFromUser() {
        return isset($this->receive['FromUserName']) ? $this->receive['FromUserName'] : '';
    }
    
    public function getReceiveToUser() {
        return isset($this->receive['ToUserName']) ? $this->receive['ToUserName'] : '';
    }

    public function getReceiveInfoType() {
        return isset($this->receive['InfoType']) ? $this->receive['InfoType'] : '';
    }

    /**
     * 
     * @return mixed string|boolean
     */
    public function getComponentVerifyTicket() {
        if ($this->getReceiveInfoType() !== 'component_verify_ticket') {
            return false;
        }
        return isset($this->receive['ComponentVerifyTicket']) ? $this->receive['ComponentVerifyTicket'] : false;
    }

    public function isEventReceived() {
        return $this->getReceiveType() == 'event';
    }

    public function isTextReceived() {
        return $this->getReceiveType() == 'text';
    }

    /**
     * 
     * @return type
     */
    public function getQueryAuthCode() {
        $prefix = 'QUERY_AUTH_CODE:';

        $content = $this->getReceiveContent();
        if (strpos($content, $prefix) === false) {
            return;
        }
        return str_replace($prefix, '', $content);
    }

    /**
     * 
     * @param type $toUser
     * @param type $content
     */
    public function sendMessage($toUser, $content) {
        $msg = array(
            'ToUserName' => $toUser,
            'FromUserName' => $this->clientAppid,
            'CreateTime' => time(),
            'MsgType' => '',
            'Content' => $content
        );

        $xml = XML::arrayToXml($msg);

        $encodeMsg = $this->encodeMessage($xml);

        echo $encodeMsg;
        exit;
    }

}
